package dynamic_handle;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import DB.DB_connection_pool;


public class Attention_user extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		PrintWriter out = response.getWriter();
		String user_account = request.getSession().getAttribute("user_account").toString();
		
		String select_user = "select * from attention where attention_user_id = '"+user_account+"'";
		JSONObject jsonObj_r = new JSONObject();
		try {
			Connection conn = DB_connection_pool.data_pool.getConnection();
			PreparedStatement ps_select_user = conn.prepareStatement(select_user);
			ResultSet rs_user = ps_select_user.executeQuery();
			JSONArray my_attention_Array = new JSONArray();
			while(rs_user.next()){
				JSONObject jsonObj = new JSONObject();
				 String select_attention_user = "select * from user_data where user_account = '"+rs_user.getString("passive_attention_user_id")+"'";
			      PreparedStatement ps_select_attention_user = conn.prepareStatement(select_attention_user);
				  ResultSet rs_attention_user = ps_select_attention_user.executeQuery();
			      if(rs_attention_user.next()){
			    	    jsonObj.put("user_account", rs_attention_user.getString("user_account"));                        //用户账号
						jsonObj.put("user_nickname", rs_attention_user.getString("user_nickname"));                    //用户昵称
						jsonObj.put("user_photo_path", rs_attention_user.getString("user_photo_path"));                //用户头像URL
			      }
				 
			      my_attention_Array.put(jsonObj);
				 
			      rs_attention_user.close();
			      ps_select_attention_user.close();
			}
			
			jsonObj_r.put("result", true);
			jsonObj_r.put("my_attention_Array", my_attention_Array);
			
			rs_user.close();
			ps_select_user.close();
			conn.close();
			
		} catch (SQLException e) {
			jsonObj_r.put("result", false);
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		out.print(jsonObj_r.toString());
	}

}
